package Screens;
import Utils.GameUtils;
import Utils.SoundManager;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.acumenvn.pent.MyPent;


public class MainMenuScreen implements Screen {
	
	Sound mClickSound;
	Sound mSelectSound;
	MyPent mGame;
	Texture mTextureBackground; // Background Texture
	TextureRegion mTextureRegionBackground;
	Texture mTextureMainMenu; // Main Menu Texture
	TextureRegion mTextureRegionMainMenu;
	Texture mTextureMenuSelector; // Menu Selector Texture
	TextureRegion mTextureRegionMenuSelector;
	SpriteBatch mBatch;
	Sprite mSpriteBackground;
	Sprite mSpriteMainMenu;
	Sprite mSpriteMenuSelector;
	int mMenuSelectorIndex = 0;
	int mJumpMenu = 60;
	int mMenu_Y = 0;
	Boolean pressed = false;
	int mMenu_X = 0;
	
	InputProcessor mInputP = new InputProcessor() {
		
		@Override
		public boolean touchUp(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean touchDragged(int screenX, int screenY, int pointer) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean touchDown(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean scrolled(int amount) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean mouseMoved(int screenX, int screenY) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyUp(int keycode) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyTyped(char character) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyDown(int keycode) {
			// TODO Auto-generated method stub
			if(keycode == Keys.UP || keycode == Keys.DOWN) {
				if(keycode == Keys.UP) {
					mMenuSelectorIndex--;
					mMenu_Y += mJumpMenu;
				} 
				if(keycode == Keys.DOWN) {
					mMenuSelectorIndex++;
					mMenu_Y -= mJumpMenu;
				}
				SoundManager.getInstance().playSound(mClickSound);
				handleMenuPointer();
			}
			
			if(keycode == Keys.ENTER) {
				SoundManager.getInstance().playSound(mSelectSound);
				handleSelector();
			}
			return false;
		}
	};
	
	public MainMenuScreen(MyPent mGame) {
		// TODO Auto-generated constructor stub
		this.mGame = mGame;
	}

	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		
		Gdx.gl10.glClearColor(0, 0, 0, 1);
		Gdx.gl10.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		mBatch.begin();
		mBatch.draw(mTextureRegionBackground, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		mBatch.draw(mTextureRegionMainMenu, 262, 220, 258, 238);
		mBatch.draw(mTextureRegionMenuSelector, 262, 220, 32, 32);
		mBatch.end();
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		mMenu_X = 235;
		mMenu_Y = 295;
		// Background
		mTextureBackground = new Texture("ImagesPOT/MenuBG.png");
		mTextureBackground.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		mTextureRegionBackground = new TextureRegion(mTextureBackground, 0, 0, 800, 600);
		
		// Menu
		mTextureMainMenu = new Texture("ImagesPOT/MainMenuContext.png");
		mTextureMainMenu.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		mTextureRegionMainMenu = new TextureRegion(mTextureMainMenu, 0, 0, 233, 238);
		// Menu Selector
		mTextureMenuSelector = new Texture("ImagesPOT/BorderInGame.png");
		mTextureMenuSelector.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		mTextureRegionMenuSelector = new TextureRegion(mTextureMenuSelector, 0, 0, 32, 32);
		
		mBatch = new SpriteBatch();
		mMenuSelectorIndex = 0;
		Gdx.graphics.setContinuousRendering(false);
		Gdx.graphics.requestRendering();
		
		// Sound
		mSelectSound = SoundManager.getInstance().getSoundSelectMenu();
		mClickSound = SoundManager.getInstance().getSoundSwitchMenu();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		mTextureBackground.dispose();
		mTextureMainMenu.dispose();
		mTextureMenuSelector.dispose();
	}
	
	private void handleMenuPointer() {
		
		if(mMenuSelectorIndex > 3) {
			mMenuSelectorIndex = 0;
			mMenu_Y = 295;
		}
		
		if(mMenuSelectorIndex < 0) {
			mMenuSelectorIndex = 3;
			mMenu_Y = 115;
		}
//		SoundManager.getInstance().stopSound(mClickSound);
	}
	
	private void handleSelector() {
		switch (mMenuSelectorIndex) {
		case 0:
			// Start game
			GameUtils.getInstance().setMapIndex(1);
			this.mGame.setScreen(new StoryScreen(this.mGame));
			break;
		case 1:
			// Continue
			break;
		case 2:
			// About
			this.mGame.setScreen(new AboutScreen(this.mGame));
			break;
		case 3:
			// Exit
			Gdx.app.exit();
			break;
		default:
			break;
		}
	}

}
